package cn.pa;

public class Sync2Test implements Runnable {

  // 静态变量
  public static int TEST_INT = 0;

  // 被同步的实例方法
  public void increase() {
    while (true) {
      if (TEST_INT < 100000) {
        synchronized (this) {
          if (TEST_INT < 100000) {

            TEST_INT++;
            System.out.println(Thread.currentThread().getName() + " TEST_INT " + TEST_INT);
          } else {
            break;
          }
        }
      } else {
        break;
      }
    }
  }

  @Override
  public void run() {
    increase();
  }

  public static void main(String[] args) throws InterruptedException {
    // 实例化对象
    Sync2Test instance = new Sync2Test();
    Thread t1 = new Thread(instance);
    t1.setName("T1");
    Thread t2 = new Thread(instance);
    t2.setName("T2");
    t1.start();
    t2.start();

    System.out.println("increase result:" + Sync2Test.TEST_INT);
  }
}
